<?php
namespace Home\Controller;
use Think\Controller;
use Think\Image;

class IndexController extends Controller {
    public function index(){
        $this->display();
    }
    public  function  register()
    {
        if($_POST['password1']!=$_POST['password2'])
        {
            $this->error('两次输入的密码不一致，请重新输入');
        }
        $m=M('users');
        $username=$_POST['username'];
        $data['name']=$username;
        $count=$m->where($data)->select();
        if($count>0)
        {
            $this->error('用户名已存在，另取用户名');
        }

        $data['name']=$_POST['username'];
        $data['password']=$_POST['password1'];
        $m->add($data);
        $this->success('注册成功',"goin");
    }
    public  function goin()
    {


       ;
    $this->display();

    }
    public function username()
    {
        $username=$_GET['username'];
        $m=M('users');
        $data=$m->field('name')->select();
        foreach($data as $value)
        {
            if($value['username']==$username)
            }
        }


        echo $username;
    }

   public  function  goinindex()
   {
       session_start();
       $m=M('users');
       $name=$_POST['username'];
       $password=$_POST['password'];
       $where['name']=$name;
      $arr= $m->where($where)->find();
       if($arr['password']==$password)
       {
           $_SESSION['username']=$_POST['username'];
           $this->success("登录成功","show_broad");
       }

       else
       {
           $this->error("用户名或密码不正确");
       }
   }
    public  function  _before_main()
    {
        session_start();
        if($_SESSION["username"]==""||!isset($_SESSION['username']))
        {
            $this->redirect('goin');
        }
    }
    public  function  main()
    {
        $this->display();
    }
    public  function  mainindex()
    {
      $broad=$_POST['broad'];
        $m=M('title');
        $data['title']=$broad;
        $count=$m->add($data);
        if($count>0)
            $this->success('success','show_broad');
        else
            error('fail');
    }
    public  function  _before_show_broad()
    {
        session_start();
        if($_SESSION["username"]==""||!isset($_SESSION['username']))
        {
            $this->redirect('goin');
        }
    }

    public  function  show_broad()
    {
        $data=0;
        $m=M('title');
        $data=$m->select();
       // var_dump($data);
        $this->assign('data',$data);
       $this->display();
    }

}
